﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


using ZhimeiSoft.Framework.Client.Base;
using ZhimeiSoft.CharmManager.DOL;
using ZhimeiSoft.Framework.Client.Utils;
using ZhimeiSoft.CharmManager.Client.Manager;
using ZhimeiSoft.CharmManager.Client.Interface;

namespace ZhimeiSoft.CharmManager.Client.Forms
{
    public partial class frmTodayReservationFinished : FormBase
    {
        /// <summary>
        /// 预约的Dataset
        /// </summary>
        private DataTable dtReservation = new DataTable();
        private DataTable dtReservationServiceItem = new DataTable();
        private DataSet dsCustomInfo = new DataSet();
        private DataSet dsEmployee = new DataSet();
        private SortedList<int, string> srotEmployeeName = new SortedList<int, string>();
        private DataSet dsServiceItemConsume = new DataSet();

        private DBEntityCustomInfo customInfo = new DBEntityCustomInfo();
        private DBEntityReservation reservationInfo = new DBEntityReservation();

        private bool isReadonly = false;

        private string error = string.Empty;

        public frmTodayReservationFinished(DBEntityReservation reservation)
        {
            InitializeComponent();

            this.reservationInfo = (DBEntityReservation)reservation.Clone();
            cbxRState.Items.Add("预约完成");
            cbxRState.Items.Add("预约过期");
            cbxRState.Items.Add("预约进行中");
            if (reservationInfo.RSTATE.Equals(DBEntityReservation.ENUM_RSTATE.C0A))
            {
                isReadonly = true;
                btnSave.Enabled = !isReadonly;
                MsgBox.MsgInfo("当前预约已经预约完成，只能进行查看不能够进行调整。");
            }
        }

        private void initControl()
        {
            //1、装载预约信息
            //2、双击某条记录可以进行修改，并按照状态进行保存
            //3、点击保存后进行检查直接提交到数据库
            //dsReservation table[0] 表示预约项目  table[1]预约项目主表
            string error = "";
            DataSet dsReservation = new DataSet();
            if (!BussinessProxyManager.Instance.QueryReservationServiceItem(reservationInfo.RESERVATIONID, out dsReservation, out error))
            {
                MsgBox.MsgInfo("获取预约信息错误：" + error);
                return;
            }
            dtReservation = dsReservation.Tables[1].Copy();
            dtReservationServiceItem = dsReservation.Tables[0].Copy();
            //reservationInfo = new DBEntityReservation(dtReservation.Rows[0]);

            if (!BussinessProxyManager.Instance.QueryCustomerID(reservationInfo.CUSTOMERID, out dsCustomInfo, out error))
            {
                MsgBox.MsgInfo("获取用户信息错误：" + error);
                return;
            }
            string customServiceReservationID = "";
            foreach (DataRow dr in dtReservationServiceItem.Rows)
            {
                customServiceReservationID += ((customServiceReservationID == string.Empty) ? "" : ",") + dr[DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID].ToString();
            }
            string sfilter = DBEntityCustomerServiceItemReservationTime.FieldName.CUSTOMERSERVICEITEMID + " IN ( " + customServiceReservationID + ") ";
            DataSet dsCustSvrItemReservationTime = new DataSet();
            if (!BussinessProxyManager.Instance.QueryCustomerServiceItemReservationTime(sfilter, out dsCustSvrItemReservationTime, out error))
            {
                MsgBox.MsgInfo("获取用户会员预约时间表错误：" + error);
                return;
            }

            if (!BussinessProxyManager.Instance.QeuryCustomServiceItemConsumeReservationID(reservationInfo.RESERVATIONID, out dsServiceItemConsume, out error))
            {
                MsgBox.MsgInfo("获取用户消费信息错误：" + error);
                return;
            }

            //增加扣减次数字段
            dtReservationServiceItem.Columns.Add(new DataColumn(DBEntityCustomerServiceItemConsume.FieldName.CMINUSCOUNT, typeof(Int32)));
            //增加护理间隔天数字段
            dtReservationServiceItem.Columns.Add(new DataColumn(DBEntityCustomerServiceItemReservationTime.FieldName.CDAYS, typeof(Int32)));
            //增加下次预约时间字段
            dtReservationServiceItem.Columns.Add(new DataColumn(DBEntityReservation.FieldName.RESERVATIONTIME, typeof(DateTime)));

            customInfo = new DBEntityCustomInfo(dsCustomInfo.Tables[0].Rows[0]);
            txtCustomName.Text = dsCustomInfo.Tables[0].Rows[0][DBEntityCustomInfo.FieldName.CNAME].ToString();
            txtCustomNickName.Text = dsCustomInfo.Tables[0].Rows[0][DBEntityCustomInfo.FieldName.CNICKNAME].ToString();
            txtCustomPhone.Text = dsCustomInfo.Tables[0].Rows[0][DBEntityCustomInfo.FieldName.CMOBILEPHONE].ToString();
            //预约到店时间
            DateTime dtimeResevationTime = Convert.ToDateTime(dtReservation.Rows[0][DBEntityReservation.FieldName.RESERVATIONTIME]);
            txtReservationTime.Text = dtimeResevationTime.ToLongDateString() + " " + dtimeResevationTime.ToLongTimeString();
            //护理开始时间
            DateTime dtimeRWorkstartTime = Convert.ToDateTime(dtReservation.Rows[0][DBEntityReservation.FieldName.RESERVATIONTIME]);
            txtRWorkStartTime.Text = dtimeRWorkstartTime.ToLongDateString() + " " + dtimeRWorkstartTime.ToLongTimeString();
            //护理结束时间
            DateTime dtimeRWorkendTime = Convert.ToDateTime(dtReservation.Rows[0][DBEntityReservation.FieldName.RWORKENDTIME].ToString());
            txtRWorkEndTime.Text = dtimeRWorkendTime.ToLongDateString() + " " + dtimeRWorkendTime.ToLongTimeString();
            //预约状态
            cbxRState.Text = DBEntityReservation.ENUM_RSTATE.EnumStrToChineseStr(dtReservation.Rows[0][DBEntityReservation.FieldName.RSTATE].ToString());
            //
            memRMemo.Text = dtReservation.Rows[0][DBEntityReservation.FieldName.RMEMO].ToString();
            foreach (DataRow dr in dtReservationServiceItem.Rows)
            {
                DataRow[] drServcieConsume = dsServiceItemConsume.Tables[0].Select(DBEntityCustomerServiceItemConsume.FieldName.CUSTOMERSERVICEITEMID + " =  " + dr[DBEntityCustomerServiceItemConsume.FieldName.CUSTOMERSERVICEITEMID].ToString());
                if (drServcieConsume.Length != 0)
                {
                    dr[DBEntityCustomerServiceItemConsume.FieldName.CMINUSCOUNT] = drServcieConsume[0][DBEntityCustomerServiceItemConsume.FieldName.CMINUSCOUNT];
                }
            }
            gridReservation.DataSource = dtReservationServiceItem;

        }

        private void frmTodayReservationFinished_Shown(object sender, EventArgs e)
        {

            initControl();

        }

        private void cavReservation_DoubleClick(object sender, EventArgs e)
        {
            if (cavReservation.FocusedRowHandle < 0)
            {
                return;
            }

            DataRow drReservationInfo = cavReservation.GetDataRow(cavReservation.FocusedRowHandle);
            if (drReservationInfo[DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE].ToString() == DBEntityReservationServiceItem.ENUM_RESERVATIONSTATE.C0A)
            {
                //服务完成就是只读
                isReadonly = true;
            }
            else
            {
                isReadonly = false;
            }
            frmReservationCustomFinished frmFinished = new frmReservationCustomFinished(reservationInfo, drReservationInfo, isReadonly);
            if (frmFinished.ShowDialog() == DialogResult.Cancel)
            {
                return;
            }

            initControl();
            string error = string.Empty;
            if (checkReservationOver(dtReservationServiceItem, out error))
            {
                //检测所有的项目都预约完成后自动完成保存
                reservationInfo.RSTATE = DBEntityReservation.ENUM_RSTATE.C0A;
                if (!BussinessProxyManager.Instance.UpdateReservation(reservationInfo, null, out error))
                {
                    MsgBox.MsgInfo("保存预约信息错误：" + error);
                    return;
                }
                this.DialogResult = DialogResult.OK;
            }
        }

        private void cavReservation_CustomDrawCardFieldValue(object sender, DevExpress.XtraGrid.Views.Base.RowCellCustomDrawEventArgs e)
        {
            Rectangle r = e.Bounds;
            string dispTxt = string.Empty;
            if (e.Column.FieldName.Equals(DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE))
            {
                dispTxt = DBEntityReservationServiceItem.ENUM_RESERVATIONSTATE.EnumStrToChineseStr(e.CellValue.ToString());
                e.Style.DrawString(e.Graphics, dispTxt, r);
                e.Handled = true;
            }
            else if (e.Column.FieldName.Equals(DBEntityEmployee.FieldName.EMPLOYEEID))
            {
                dispTxt = srotEmployeeName[Convert.ToInt32(e.CellValue)].ToString();
                e.Style.DrawString(e.Graphics, dispTxt, r);
                e.Handled = true;
            }
        }

        private void frmTodayReservationFinished_Load(object sender, EventArgs e)
        {
            if (!BussinessProxyManager.Instance.QueryEmployeeState(string.Empty, out dsEmployee, out error))
            {
                MsgBox.MsgInfo("初始化员工信息错误：" + error);
                return;
            }
            foreach (DataRow drEmployee in dsEmployee.Tables[0].Rows)
            {
                srotEmployeeName.Add(Convert.ToInt32(drEmployee[DBEntityEmployee.FieldName.EMPLOYEEID]), drEmployee[DBEntityEmployee.FieldName.ENAME].ToString());
            }
        }

        private void buttonX1_Click(object sender, EventArgs e)
        {
            string error = string.Empty;            
            if (cbxRState.SelectedItem.ToString() == DBEntityReservation.ENUM_RSTATE.C0A_CHINESE)
            {
                if (!checkReservationOver(dtReservationServiceItem, out error)) {
                    MsgBox.MsgInfo(error);
                    return;
                }
            }
            reservationInfo.RSTATE = DBEntityReservation.ENUM_RSTATE.ChineseStrToEnumStr(cbxRState.SelectedItem.ToString());
            if (!BussinessProxyManager.Instance.UpdateReservation(reservationInfo, null, out error))
            {
                MsgBox.MsgInfo("保存预约信息错误：" + error);
                return;
            }
            this.DialogResult = DialogResult.OK;
        }
        /// <summary>
        /// 检查当前项目是否完全预约
        /// </summary>
        /// <param name="drReservationServiceItem"></param>
        /// <param name="error"></param>
        /// <returns></returns>
        private bool checkReservationOver(DataTable dtReservationServiceItem, out string error)
        {
            error = string.Empty;
            DataRow[] drReservationDetail = dtReservationServiceItem.Select("", "", DataViewRowState.CurrentRows);
            foreach (DataRow dr in drReservationDetail)
            {
                if (!(dr[DBEntityReservationServiceItem.FieldName.RESERVATIONSTATE].ToString() == DBEntityReservationServiceItem.ENUM_RESERVATIONSTATE.C0A))
                {
                    error = "服务项目： [" + dr[DBEntityServiceItem.FieldName.SERVICEITEMCODE] + "]" + dr[DBEntityServiceItem.FieldName.SNAME].ToString() + " 还没有预约完成，请预约完成后在进行保存。";
                    return false;
                }
            }
            return true;
        }

        private void buttonX2_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
        }
    }
}
